package com.example.common.util;

import java.util.UUID;

/**
 * 请求上下文工具类
 * 用于管理请求的requestId和version
 * 
 * @author AI Assistant
 * @since 1.0.0
 */
public class RequestContext {
    
    private static final ThreadLocal<String> REQUEST_ID = new ThreadLocal<>();
    private static final ThreadLocal<String> VERSION = new ThreadLocal<>();
    
    /**
     * 设置请求ID
     */
    public static void setRequestId(String requestId) {
        REQUEST_ID.set(requestId);
    }
    
    /**
     * 获取请求ID
     */
    public static String getRequestId() {
        return REQUEST_ID.get();
    }
    
    /**
     * 生成新的请求ID
     */
    public static String generateRequestId() {
        String requestId = "req-" + System.currentTimeMillis() + "-" + UUID.randomUUID().toString().substring(0, 8);
        setRequestId(requestId);
        return requestId;
    }
    
    /**
     * 设置版本号
     */
    public static void setVersion(String version) {
        VERSION.set(version);
    }
    
    /**
     * 获取版本号
     */
    public static String getVersion() {
        return VERSION.get();
    }
    
    /**
     * 获取默认版本号
     */
    public static String getDefaultVersion() {
        return "1.0.0";
    }
    
    /**
     * 清理线程本地变量
     */
    public static void clear() {
        REQUEST_ID.remove();
        VERSION.remove();
    }
}
